from flask import render_template, g, flash,json,redirect
from appli import app,PrintInCharte,database,gvg,gvp,user_datastore,DecodeEqualList,ScaleForDisplay,ntcv,ErrorFormat
from appli.database import GetAll,GetClassifQualClass,ExecSQL,db,GetAssoc
from flask_login import current_user
from flask import render_template,  flash,request,g
from pathlib import Path
import appli,logging,appli.part.uvp_sample_import as sample_import
import appli.part.database as partdatabase,collections,re,csv
from flask_security import login_required
from wtforms  import Form, BooleanField, StringField, validators,DateTimeField,IntegerField,FloatField,SelectField,TextAreaField

class UvpPrjForm(Form):
    ptitle = StringField("颗粒物项目名称")
    rawfolder = StringField("rawfolder")
    ownerid = StringField("项目所有者")
    projid = StringField("Ecotaxa项目")
    instrumtype = StringField("设备类型")
    op_name = StringField("操作者")
    op_email = StringField("操作者邮箱")
    cs_name = StringField("首席科学家")
    cs_email = StringField("首席科学家邮箱")
    do_name = StringField("数据所有者姓名")
    do_email = StringField("数据所有者邮箱")
    prj_acronym = StringField("项目首字母缩写")
    cruise = StringField("航次")
    ship = StringField("船名")
    default_instrumsn = StringField("默认的设备序列号")
    default_depthoffset = FloatField("默认深度偏移量",[validators.Optional(strip_whitespace=True)])
    prj_info = TextAreaField("项目信息")
    public_visibility_deferral_month = IntegerField("隐私延迟", [validators.Optional(strip_whitespace=True)])
    public_partexport_deferral_month = IntegerField("一般下载延迟", [validators.Optional(strip_whitespace=True)])
    public_zooexport_deferral_month = IntegerField("浮游生物注释下载延迟", [validators.Optional(strip_whitespace=True)])


@app.route('/part/prjedit/<int:pprojid>',methods=['get','post'])
@login_required
def part_prjedit(pprojid):
    g.headcenter="<h3>颗粒物项目元信息管理</h3>"
    if pprojid>0:
        model = partdatabase.part_projects.query.filter_by(pprojid=pprojid).first()
        if model.ownerid!=current_user.id and not current_user.has_role(database.AdministratorLabel):
            return PrintInCharte(ErrorFormat("Access Denied"))
    else:
        if not (current_user.has_role(database.AdministratorLabel) or current_user.has_role(database.ProjectCreatorLabel)):
            return PrintInCharte(ErrorFormat("Access Denied"))
        model=partdatabase.part_projects()
        model.pprojid=0
        model.ownerid=current_user.id
        model.default_depthoffset=1.2
        model.public_visibility_deferral_month= app.config.get('PART_DEFAULT_VISIBLE_DELAY', '')
        model.public_partexport_deferral_month=app.config.get('PART_DEFAULT_GENERAL_EXPORT_DELAY', '')
        model.public_zooexport_deferral_month = app.config.get('PART_DEFAULT_PLANKTON_EXPORT_DELAY', '')

    UvpPrjForm.ownerid=SelectField('项目所有者',choices=database.GetAll("SELECT id,name FROM users ORDER BY trim(lower(name))"),coerce=int )
    UvpPrjForm.projid=SelectField('Ecotaxa项目',choices=[(0,''),(-1,'Create a new EcoTaxa project')]+database.GetAll("SELECT projid,concat(title,' (',cast(projid as varchar),')') FROM projects ORDER BY lower(title)"),coerce=int )
    form=UvpPrjForm(request.form,model)
    if gvp('delete')=='Y':
        try:
            db.session.delete(model)
            db.session.commit()
            return redirect("/part/prj/")
        except:
            flash("You can delete a project only if doesn't have any data (sample,...)",'error')
            db.session.rollback()
            return redirect("/part/prj/" + str(model.pprojid))
    if request.method == 'POST' and form.validate():
        if pprojid==0:
            model.pprojid = None
            db.session.add(model)
        for k,v in form.data.items():
            setattr(model,k,v)
        if model.projid==0: # 0 permet de dire aucun projet WTForm ne sait pas gére None avec coerce=int
            model.projid=None
        if model.projid == -1: # création d'un projet Ecotaxa
            model.projid = None
            EcotaxaProject=database.Projects()
            EcotaxaProject.title=model.ptitle # Nommé comme le projet Particle
            db.session.add(EcotaxaProject)
            db.session.commit()
            EcotaxaProjectMember=database.ProjectsPriv()
            EcotaxaProjectMember.projid=EcotaxaProject.projid # L'utilisateur courant est Manager de ce projet
            EcotaxaProjectMember.member=current_user.id
            EcotaxaProjectMember.privilege='Manage'
            db.session.add(EcotaxaProjectMember)
            model.projid = EcotaxaProject.projid # On affecte le nouveau projet au projet Particle.
        db.session.commit()
        return redirect("/part/prj/"+str(model.pprojid))
    return PrintInCharte(render_template("part/prjedit.html", form=form, prjid=model.pprojid))


@app.route('/part/readprojectmeta',methods=['get','post'])
@login_required
def part_readprojectmeta():
    res={}
    ServerRoot = Path(app.config['SERVERLOADAREA'])
    DossierUVPPath = ServerRoot / gvg('path')
    DirName = DossierUVPPath.name
    CruiseFile=DossierUVPPath/"config/cruise_info.txt"
    # app.logger.info("CruiseFile=%s",CruiseFile.as_posix())
    if CruiseFile.exists():
        CruiseInfoData = appli.DecodeEqualList(CruiseFile.open('r').read())
        res['op_name']=CruiseInfoData.get('op_name')
        res['op_email'] = CruiseInfoData.get('op_email')
        res['cs_name']=CruiseInfoData.get('cs_name')
        res['cs_email'] = CruiseInfoData.get('cs_email')
        res['do_name']=CruiseInfoData.get('do_name')
        res['do_email'] = CruiseInfoData.get('do_email')
        res['prj_info']=CruiseInfoData.get('gen_info')
        res['prj_acronym'] = CruiseInfoData.get('acron')
    # ConfigFile = DossierUVPPath / "config/uvp5_settings/uvp5_configuration_data.txt"
    # if ConfigFile.exists():
    #     ConfigInfoData = appli.DecodeEqualList(ConfigFile.open('r').read())
    #     res['default_aa']=ConfigInfoData.get('aa_calib')
    #     res['default_exp'] = ConfigInfoData.get('exp_calib')
    #     res['default_volimage'] = ConfigInfoData.get('img_vol')

    m = re.search(R"([^_]+)_(.*)", DirName)
    if m.lastindex == 2:
        FichierHeader = DossierUVPPath / "meta" / (m.group(1) + "_header_" + m.group(2) + ".txt")
        res['instrumtype'] = m.group(1)
        m = re.search(R"([^_]+)", m.group(2))
        res['default_instrumsn'] = m.group(1)
        if FichierHeader.exists():
            LstSamples=[]
            with FichierHeader.open() as FichierHeaderHandler:
                F = csv.DictReader(FichierHeaderHandler, delimiter=';')
                for r in F:
                    LstSamples.append(r)
                    # print(LstSamples)
            if len(LstSamples) > 0:
                res['cruise'] = LstSamples[0].get('cruise')
                res['ship'] = LstSamples[0].get('ship')
    res['default_depthoffset']=1.2
    return json.dumps(res)

